package com.hspedu.newData.dmsxl.数组.模拟;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC59螺旋矩阵Ⅱ {

    public int[][] generateMatrix(int n) {

        int[][] result = new int[n][n];

        int low = 0;
        int high = n - 1;
        int count = 1;
        while (low < high){


            for (int i = low; i < high; i++) {
                result[low][i] = count++;
            }
            for (int i = low; i < high; i++) {
                result[i][high] = count++;
            }
            for (int i = high; i > low ; i--) {
                result[high][i] = count++;
            }
            for (int i = high; i > low; i--) {
                result[i][low] = count++;
            }
            low++;
            high--;
        }

        if (low == high){
            result[low][high] = count;
        }

        return result;
    }


    @Test
    public void test() {
        // 测试代码
        generateMatrix(1);
        generateMatrix(3);
        generateMatrix(4);

    }
}
